# Maintainer: Yusuke Sasaki <y_sasaki@nuem.nagoya-u.ac.jp>

_realname=gtest
pkgbase="mingw-w64-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.8.1
pkgrel=2
pkgdesc="Google Test (mingw-w64)"
arch=('any')
url="https://github.com/google/googletest"
license=('custom:BSD3')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-cmake")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/google/googletest/archive/release-${pkgver}.tar.gz")
sha256sums=('9bf1fe5182a604b4135edc1a425ae356c9ad15e9b23f9f12a02e80184c3a249c')

build() {
  cd "${srcdir}/googletest-release-${pkgver}"

  rm -rf build-${CARCH}
  rm -rf fused-gtest-src
  rm -rf fused-gmock-src
  mkdir build-${CARCH} && cd build-${CARCH}

  cmake -G "Unix Makefiles" -DBUILD_SHARED_LIBS=ON -DCMAKE_SKIP_RPATH=ON ..
  make

  ../googletest/scripts/fuse_gtest_files.py ../googletest ../fused-gtest-src
  ../googlemock/scripts/fuse_gmock_files.py ../googlemock ../fused-gmock-src
}

package() {
  cd "${srcdir}/googletest-release-${pkgver}"

  mkdir -pm 0755 "$pkgdir"/${MINGW_PREFIX}/{bin,lib/pkgconfig,include/gtest/internal/custom,include/gmock/internal/custom,share/licenses/${_realname},src/gtest/src,src/gmock/src,src/gtest/cmake}
  install -m 0644 build-${CARCH}/googlemock/gtest/libgtest{,_main}.dll "$pkgdir"/${MINGW_PREFIX}/bin
  install -m 0644 build-${CARCH}/googlemock/gtest/libgtest{,_main}.dll.a "$pkgdir"/${MINGW_PREFIX}/lib
  install -m 0644 build-${CARCH}/googlemock/libgmock{,_main}.dll "$pkgdir"/${MINGW_PREFIX}/bin
  install -m 0644 build-${CARCH}/googlemock/libgmock{,_main}.dll.a "$pkgdir"/${MINGW_PREFIX}/lib
  install -m 0644 googletest/include/gtest/*.h "$pkgdir"/${MINGW_PREFIX}/include/gtest
  install -m 0644 googletest/include/gtest/internal/*.h "$pkgdir"/${MINGW_PREFIX}/include/gtest/internal/
  install -m 0644 googletest/include/gtest/internal/custom/*.h "$pkgdir"/${MINGW_PREFIX}/include/gtest/internal/custom/
  install -m 0644 googlemock/include/gmock/*.h "$pkgdir"/${MINGW_PREFIX}/include/gmock
  install -m 0644 googlemock/include/gmock/internal/*.h "$pkgdir"/${MINGW_PREFIX}/include/gmock/internal/
  install -m 0644 googlemock/include/gmock/internal/custom/*.h "$pkgdir"/${MINGW_PREFIX}/include/gmock/internal/custom/
  install -m 0644 googletest/LICENSE "$pkgdir"/${MINGW_PREFIX}/share/licenses/${_realname}/
  install -m 0644 fused-gtest-src/gtest/* "$pkgdir"/${MINGW_PREFIX}/src/gtest/src/
  install -m 0644 fused-gmock-src/gmock/* "$pkgdir"/${MINGW_PREFIX}/src/gmock/src/
  install -m 0644 fused-gmock-src/*.cc "$pkgdir"/${MINGW_PREFIX}/src/gmock/src/
  install -m 0644 CMakeLists.txt "$pkgdir"/${MINGW_PREFIX}/src/gtest/
  install -m 0644 googletest/cmake/* "$pkgdir"/${MINGW_PREFIX}/src/gtest/cmake/
  
  # install pkg-config files
  install -m 0644 build-${CARCH}/googlemock/gtest/generated/*pc ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/
  for _ff in ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/*.pc; do
    sed -e "/libdir=/c libdir=${MINGW_PREFIX}/lib" \
        -e "/includedir=/c includedir=${MINGW_PREFIX}/include"  \
        -i ${_ff}
  done
}

